package com.portal.cwegportal.controller;

import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import com.portal.cwegportal.input.newscollege.*;
import com.portal.cwegportal.model.NewsCollege;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.Random;
import java.util.ResourceBundle;

@RestController
@RequestMapping("api/services/portal/newscollege")
@Api(description ="新闻/学院天地API")
public class NewsCollegeController {
    @Autowired
    com.portal.cwegportal.service.NewsCollegeService newsCollegeService;

    @Autowired
    com.portal.cwegportal.util.Upload uploadUtil;

    @ApiOperation(value = "创建新闻/学院天地", notes = "创建新闻/学院天地", produces = "application/json")
    @PostMapping("/createNewsCollege")
    public ResponseEntity<JsonResult> createNewsCollege(@RequestBody CreateNewsCollegeInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"创建新闻/学院天地",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(newsCollegeService.createNewsCollege(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "删除新闻/学院天地", notes = "删除新闻/学院天地", produces = "application/json")
    @PostMapping("/deleteNewsCollege")
    public ResponseEntity<JsonResult> deleteNewsCollege(@RequestBody DeleteNewsCollegeInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"删除新闻/学院天地",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(newsCollegeService.deleteNewsCollege(input));

        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "修改新闻/学院天地", notes = "修改新闻/学院天地", produces = "application/json")
    @PostMapping("/editNewsCollege")
    public ResponseEntity<JsonResult> editNewsCollege(@RequestBody EditNewsCollegeInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"修改新闻/学院天地",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(newsCollegeService.editNewsCollege(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "查詢新闻/学院天地", notes = "查詢新闻/学院天地", produces = "application/json")
    @PostMapping("/getNewsCollege")
    public ResponseEntity<JsonResult> getNewsCollege(@RequestBody GetNewsCollegeInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"查詢新闻/学院天地",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(newsCollegeService.getNewsCollege(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "加载新闻/学院天地", notes = "加载新闻/学院天地", produces = "application/json")
    @PostMapping("/pageLoad")
    public ResponseEntity<JsonResult> pageLoad(@RequestBody PageLoadInput input) throws Exception {
        BusinessLogger.Info( LoggerGenre.Add,"加载新闻/学院天地",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(newsCollegeService.pageLoad(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "通过id查询新闻/学院天地", notes = "通过id查询新闻/学院天地", produces = "application/json")
    @PostMapping("/getNewsCollegeById")
    public ResponseEntity<JsonResult> getNewsCollegeById(@RequestBody GetNewsCollegeInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"通过id查询新闻/学院天地",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(newsCollegeService.getNewsCollegeById(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "首页新闻图片", notes = "首页新闻图片", produces = "application/json")
    @PostMapping("/getHomePageNewsPic")
    public ResponseEntity<JsonResult> getHomePageNewsPic(@RequestBody GetHomePageNewsCollegeInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"首页新闻图片",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(newsCollegeService.getHomePageNewsPic(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "首页查詢新闻/学院天地", notes = "首页查詢新闻/学院天地", produces = "application/json")
    @PostMapping("/getHomePageNewsCollege")
    public ResponseEntity<JsonResult> getHomePageNewsCollege(@RequestBody GetHomePageNewsCollegeInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"首页查詢新闻/学院天地",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(newsCollegeService.getHomePageNewsCollege(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "创建新闻/学院天地——上传", notes = "创建新闻/学院天地——上传", produces = "application/json")
    @PostMapping("/upload")
    public String upload(@RequestParam("myFileName") MultipartFile imageFile, HttpServletRequest request, HttpServletResponse response)throws Exception{
        //System.out.println("开始处理");
        //String fileName = uploadUtil.fileUpload(imageFile, request);
//      HttpSession session = request.getSession();
//      session.setAttribute("imageSession", fileName);

        String fileName = null;
        String filePath="D:\\\\cwegImage";
        if(!imageFile.isEmpty()){

            //重新生成文件名字
            fileName = new Date().getTime()+""+new Random().nextInt()+"."+imageFile.getOriginalFilename().split("\\.")[1];
            //将文件保存到指定目录
            imageFile.transferTo(new File(filePath+"/"+fileName));
        }

        return filePath+"/"+fileName;
    }
}
